package com.arqui.admmon_module.server.handlers;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.rpc.ServiceException;
import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
import com.arqui.admmon_module.client.action.HistoricInvalid;
import com.arqui.admmon_module.client.action.HistoricInvalidResult;
import com.arqui.admmon_module.server.services.InvalidTransactionsService;
import com.arqui.admmon_module.server.webservices.eventsmonitor.NonexistentEntityException;
import com.arqui.admmon_module.server.webservices.eventsmonitor.UnknownHostException;
import com.arqui.admmon_module.shared.InvalidsEntity;
import com.google.inject.Inject;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.shared.ActionException;

public class HistoricInvalidActionHandler implements
		ActionHandler<HistoricInvalid, HistoricInvalidResult> {

	@Inject
	public HistoricInvalidActionHandler() {
	}

	@Override
	public HistoricInvalidResult execute(HistoricInvalid action,
			ExecutionContext context) throws ActionException {
		if(action.getType()=="0"){
			InvalidTransactionsService service = new InvalidTransactionsService();
			List<InvalidsEntity> result= new ArrayList<InvalidsEntity>();
			try {
				result = service.getNotSytaxTransactionHistoric(action.getStartDate(), action.getEndDate());
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (NonexistentEntityException e) {
				e.printStackTrace();
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (ServiceException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return new HistoricInvalidResult(result);
		}else{
			if(action.getType()=="1"){
				InvalidTransactionsService service = new InvalidTransactionsService();
				List<InvalidsEntity> result= new ArrayList<InvalidsEntity>();
				try {
					result = service.getNotIdentificationTransactionHistoric(action.getStartDate(), action.getEndDate());
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (NonexistentEntityException e) {
					e.printStackTrace();
				} catch (RemoteException e) {
					e.printStackTrace();
				} catch (ServiceException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return new HistoricInvalidResult(result);
			}else{
				InvalidTransactionsService service = new InvalidTransactionsService();
				List<InvalidsEntity> result= new ArrayList<InvalidsEntity>();
				try {
					result = service.getNotCardTransactionHistoric(action.getStartDate(), action.getEndDate());
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (NonexistentEntityException e) {
					e.printStackTrace();
				} catch (RemoteException e) {
					e.printStackTrace();
				} catch (ServiceException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return new HistoricInvalidResult(result);
			}
		}
	}

	@Override
	public void undo(HistoricInvalid action, HistoricInvalidResult result,
			ExecutionContext context) throws ActionException {
	}

	@Override
	public Class<HistoricInvalid> getActionType() {
		return HistoricInvalid.class;
	}
}
